<?php

declare(strict_types=1);

namespace app\common\service\crm;

use app\common\traits\ServiceTrait;
use app\common\dao\crm\CustomerDao;
use app\common\service\crm\ContactsService;

/**
 * 客户表 服务业务层(Service)
 * @author 吕玉冰
 * @date  2022-07-21
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class CustomerService
{
    protected $dao = null;

    public function __construct()
    {
        $this->dao = new CustomerDao();
        $this->contactsService = new ContactsService();
    }

    use ServiceTrait;


    public function _list_where($data)
    {
        $ids = [];
        $where = [];
        $data['type'] = isset($data['type']) ? $data['type'] : 0;
        switch ($data['type']) {
            case 0:
                break;
            case 1:
                if (!empty($data['super_id'])) {
                    $ids[] = $data['super_id'];
                }
                if (!empty($data['user_id'])) {
                    $ids[] = $data['user_id'];
                }
                $where[] = ["parent_id", "in", $ids];
                break;
        }
        $data['where'] = $where;
        return [
            'where' => isset($data['where']) ? $data['where'] : [],
            'order' => isset($data['order']) ? $data['order'] : [],
            'field' => isset($data['field']) ? $data['field'] : ['*'],
        ];
    }


    /**
     * 保存表单数据
     * @param $data
     * @return array
     */
    public function saveForm($data)
    {
        if (empty($data)) {
            return _err(1002);
        }
        if (isset($data['id']) && !empty($data['id'])) {
            $result = $this->dao->update($data['id'], $data);
            if ($result) {
                return _ok(1021);
            }
        } else {
            $result = $this->dao->add($data);
            if ($result) {
                $return = _ok(1023);
                if (!empty($result->id)) {
                    //新增联系人
                    $data['customer_id'] = $result->id;
                    $this->contactsService->auto($data);

                    $return['data'] = $result->id;
                }
                return $return;
            }
        }
        return _err(1005);
    }


}



    